#include "lvgl.h"
#include <stdio.h>
#include "gui_guider.h"
#include "events_init.h"
#include "widgets_init.h"
#include "custom.h"

void set_label_style(lv_obj_t *label) {
    lv_obj_set_style_border_width(label, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(label, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(label, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(label, &lv_font_SourceHanSerifSC_Regular_16, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(label, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_letter_space(label, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(label, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_LEFT, LV_PART_MAIN | LV_STATE_DEFAULT); // 修改为左对齐
    lv_obj_set_style_bg_opa(label, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_all(label, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_width(label, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
}


void setup_scr_screen_devinfo(lv_ui *ui) {
    // Create screen
    ui->screen_devinfo = lv_obj_create(NULL);
    lv_obj_set_size(ui->screen_devinfo, 480, 320);
    lv_obj_set_scrollbar_mode(ui->screen_devinfo, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_style_bg_opa(ui->screen_devinfo, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui->screen_devinfo, lv_color_hex(0x9efff6), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_grad_dir(ui->screen_devinfo, LV_GRAD_DIR_NONE, LV_PART_MAIN | LV_STATE_DEFAULT);

    // Create button
    ui->screen_devinfo_btn_1 = lv_btn_create(ui->screen_devinfo);
    ui->screen_devinfo_btn_1_label = lv_label_create(ui->screen_devinfo_btn_1);
    lv_label_set_text(ui->screen_devinfo_btn_1_label, "返回");
    lv_label_set_long_mode(ui->screen_devinfo_btn_1_label, LV_LABEL_LONG_WRAP);
    lv_obj_align(ui->screen_devinfo_btn_1_label, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_pad_all(ui->screen_devinfo_btn_1, 0, LV_STATE_DEFAULT);
    lv_obj_set_width(ui->screen_devinfo_btn_1_label, LV_PCT(100));
    lv_obj_set_pos(ui->screen_devinfo_btn_1, 413, 284);
    lv_obj_set_size(ui->screen_devinfo_btn_1, 63, 30);
    lv_obj_set_style_bg_opa(ui->screen_devinfo_btn_1, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui->screen_devinfo_btn_1, lv_color_hex(0x2195f6), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui->screen_devinfo_btn_1, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui->screen_devinfo_btn_1, 5, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_width(ui->screen_devinfo_btn_1, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui->screen_devinfo_btn_1, lv_color_hex(0xffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui->screen_devinfo_btn_1, &lv_font_SourceHanSerifSC_Regular_16, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui->screen_devinfo_btn_1, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui->screen_devinfo_btn_1, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);

    // Create line
    ui->screen_devinfo_line_1 = lv_line_create(ui->screen_devinfo);
    static lv_point_t screen_devinfo_line_1[] = {{0, 0}, {480, 0}};
    lv_line_set_points(ui->screen_devinfo_line_1, screen_devinfo_line_1, 2);
    lv_obj_set_pos(ui->screen_devinfo_line_1, 0, 279);
    lv_obj_set_size(ui->screen_devinfo_line_1, 478, 1);
    lv_obj_set_style_line_width(ui->screen_devinfo_line_1, 2, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_line_color(ui->screen_devinfo_line_1, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_line_opa(ui->screen_devinfo_line_1, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_line_rounded(ui->screen_devinfo_line_1, true, LV_PART_MAIN | LV_STATE_DEFAULT);

    // Create labels
    const char *label_texts[] = {
        "设备号:", "ICCID:", "服务费截止日期:", "可绑定节点数量:", "已绑定节点数量:",
        "软件版本:", "硬件版本:", "电池电压:", "报警类型:"
    };
    lv_obj_t **labels[] = {
        &ui->screen_devinfo_label_1, &ui->screen_devinfo_label_2, &ui->screen_devinfo_label_3,
        &ui->screen_devinfo_label_4, &ui->screen_devinfo_label_5, &ui->screen_devinfo_label_6,
        &ui->screen_devinfo_label_7, &ui->screen_devinfo_label_8, &ui->screen_devinfo_label_9
    };
    int y_positions[] = {18, 43, 69, 93, 118, 142, 166, 191, 216};

    for (int i = 0; i < 9; i++) {
        *labels[i] = lv_label_create(ui->screen_devinfo);
        lv_label_set_text(*labels[i], label_texts[i]);
        lv_label_set_long_mode(*labels[i], LV_LABEL_LONG_WRAP);
        lv_obj_set_pos(*labels[i], 26, y_positions[i]);
        lv_obj_set_size(*labels[i], 120, 19);
        set_label_style(*labels[i]);
    }

    // Create empty labels for dynamic data
    const char *empty_labels[] = {
        "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""
    };
    lv_obj_t **data_labels[] = {
        &ui->screen_devinfo_label_10, &ui->screen_devinfo_label_11, &ui->screen_devinfo_label_12,
        &ui->screen_devinfo_label_13, &ui->screen_devinfo_label_14, &ui->screen_devinfo_label_15,
        &ui->screen_devinfo_label_16, &ui->screen_devinfo_label_17, &ui->screen_devinfo_label_18
    };
    int data_y_positions[] = {18, 43, 69, 93, 118, 142, 166, 191, 216};

    for (int i = 0; i < 9; i++) {
        *data_labels[i] = lv_label_create(ui->screen_devinfo);
        lv_label_set_text(*data_labels[i], empty_labels[i]);
        lv_label_set_long_mode(*data_labels[i], LV_LABEL_LONG_WRAP);
        lv_obj_set_pos(*data_labels[i],150, data_y_positions[i]);
        lv_obj_set_size(*data_labels[i], 195, 16);
        set_label_style(*data_labels[i]);
    }

    // Update current screen layout.
    lv_obj_update_layout(ui->screen_devinfo);

    // Init events for screen.
    events_init_screen_devinfo(ui);
}
